<template>
    <div class="header">
        <!-- 折叠按钮 -->
        <div class="collapse-btn" @click="collapseChage">
            <i v-if="!collapse" class="el-icon-s-fold"></i>
            <i v-else class="el-icon-s-unfold"></i>
        </div>
        <div class="logo">Loongson Project 项目管理申报系统</div>
        <div class="header-right">
            <el-row style="line-height: 70px">
                <el-col :span="16">
                    <el-row>
                        <el-col :span="18">
                            <el-input  v-model="input" placeholder="请输入内容"></el-input>
                        </el-col>
                        <el-col :span="6">
                            <el-button type="primary" icon="el-icon-search">搜索</el-button>
                        </el-col>
                    </el-row>
                </el-col>
                <el-col :span="8">
                    <div class="header-user-con">
                        <!-- 消息中心 -->
                        <div class="btn-bell">
                            <el-tooltip
                                    effect="dark"
                                    :content="message?`有${message}条未读消息`:`消息中心`"
                                    placement="bottom"
                            >
                                <router-link to="/tabs">
                                    <i class="el-icon-bell"></i>
                                </router-link>
                            </el-tooltip>
                            <span class="btn-bell-badge" v-if="message"></span>
                        </div>
                        <!-- 用户头像 -->
                        <div class="user-avator">
                            <img src="../../assets/img/img.jpg" />
                        </div>
                        <!-- 用户名下拉菜单 -->
                        <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                    <span class="el-dropdown-link">
                        {{form.username}}
                        <i class="el-icon-caret-bottom"></i>
                    </span>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item @click.native="addInfo">
                                    <el-button type="text" @click="dialogFormVisible = true"> 个人信息</el-button>
                                </el-dropdown-item>
                                <el-dropdown-item divided command="loginout" @click.native="signOut">退出登录</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </div>
                </el-col>
            </el-row>
        </div>
        <el-dialog title="编辑个人信息" :visible.sync="dialogFormVisible" @closed="handleClose">
            <el-dialog
                    title="提示"
                    :visible.sync="centerDialogVisible"
                    width="30%"
                    append-to-body
                    >
                <span>确定注销该用户？</span>
                <span slot="footer" class="dialog-footer">
                        <el-button @click="centerDialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="logout">确 定</el-button>
                    </span>
            </el-dialog>
            <el-dialog
                    title="修改密码"
                    :visible.sync="updateDialogVisible"
                    width="30%"
                    append-to-body
            >
                <el-form :model="form">
                    <el-form-item label="旧密码">
                        <el-input v-model="form.oldpassword"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码">
                        <el-input v-model="form.password"></el-input>
                    </el-form-item>
                    <el-form-item label="确认新密码">
                        <el-input v-model="form.password2"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="updateDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="updatepassword">确 定</el-button>
                </span>
            </el-dialog>
            <el-form :model="form">
                <el-form-item label="用户名">
                    <el-input v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-select v-model="form.gender" value-key="value" >
                        <el-option :label="item.value" v-for="(item) in genderList" :key="item.value" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.email"></el-input>
                </el-form-item>
                <el-form-item label="团队">
                    <el-input v-model="form.team"></el-input>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="form.userPhone"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                <el-input v-model="form.location"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" style="float: left" plain @click="updateDialogVisible = true">修改密码</el-button>
                <el-button type="warning" plain style="float: left" @click="centerDialogVisible = true">注销用户</el-button>
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click.native="infochange()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import bus from '../common/bus';
import ElementUI, {Message} from 'element-ui';
export default {
    data() {
        return {
          isAddMember : false,
          updateDialogVisible:false,
          centerDialogVisible: false,
          dialogImageUrl: '',
          dialogVisible: false,
          disabled: false,
          dialogFormVisible: false,
          form: {
            oldpassword:'',
            gender:'',
            team:'',
            userPhone:'',
            location:'',
            email:'',
            username:'',
            password:'',
            password2:'',
          },
          genderList : [
            {value: '男', id: '0' },
            {value: '女', id: '1' },
            {value: '保密', id: '2' }
          ],
          formLabelWidth: '120px',
          rules: {
            name: [
              { required: true, message: '请输入活动名称', trigger: 'blur' },
              { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
            ]
          },
            isAddInfo:false,
            collapse: false,
            fullscreen: false,
            message: 2,
        };
    },
    computed: {
        username() {
            this.$http.get('/user/info/showMe')
              .then(res => {
                if (res){
                  this.$store.state.name = res.data.realname;
                }
              });
        }
    },
    methods: {
      infochange(){
        let gender = this.genderList.find( item => {
          return item.value === this.form.gender
        });
        this.$http.put('/user/info/change',{
          email:this.form.email,
          gender: gender.id,
          team:this.form.team,
          location:this.form.location,
          realname:this.form.username,
        }).then(res =>{
          console.log(this.form.gender);
          Message.success('修改成功');
          this.dialogFormVisible = false;
        })
      },
      handleRemove(file) {
        console.log(file);
      },
      //退出登录
      signOut(){
        this.$http.get('/user/logout').then(res => {
          if (res){
            localStorage.removeItem('token');
            this.$router.push('/login');
          }
        });
      },
      //注销账户
      logout(){
        this.$http.delete('/user/removeMe').then(res => {
          this.router.push('/login');
          Message.success('注销成功');
        })
      },
      //修改密码
      updatepassword(){
        if(this.form.password!==this.form.password2){
          Message.error('两次密码不一致');
        }else{
          this.$http.put('/user/reset',{
              newPasswd: this.form.password,
              oldPasswd: this.form.oldpassword
          }).then(res =>{
            console.log(res);
            Message.success('修改成功');
              this.updateDialogVisible = false
          })
        }
      },

      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      handleDownload(file) {
        console.log(file);
      },
      handleClose () {
        this.$refs.ruleForm.resetFields()
        this.form = {
          team:'',
          userPhone:'',
          location:'',
          email:'',
          name: '',
        }
      },
      handleSave () {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            console.log('输入正确')
            this.dialogFormVisible = false
          } else {
            console.log('输入错误')
          }
        })},

        addInfo(){
        this.isAddInfo=true
      },
        // 用户名下拉菜单选择事件
        handleCommand(command) {
            if (command == 'loginout') {
                localStorage.removeItem('ms_username');
                this.$router.push('/login');
            }
        },
        // 侧边栏折叠
        collapseChage() {
            this.collapse = !this.collapse;
            bus.$emit('collapse', this.collapse);
        },
        // 全屏事件
        handleFullScreen() {
            let element = document.documentElement;
            if (this.fullscreen) {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                }
            } else {
                if (element.requestFullscreen) {
                    element.requestFullscreen();
                } else if (element.webkitRequestFullScreen) {
                    element.webkitRequestFullScreen();
                } else if (element.mozRequestFullScreen) {
                    element.mozRequestFullScreen();
                } else if (element.msRequestFullscreen) {
                    element.msRequestFullscreen();
                }
            }
            this.fullscreen = !this.fullscreen;
        }
    },
    mounted() {
        if (document.body.clientWidth < 1500) {
            this.collapseChage();
        }
        this.$http.get('/user/info/showMe',{
            headers: {
              'access-token': window.localStorage.getItem('token')
            }
          }).then(res=>{
            console.log(res.data);
            this.form.team = res.data.team;
            this.form.userPhone =res.data.userPhone;
            this.form.location =res.data.location;
            this.form.email =res.data.email;
            this.form.username = res.data.realname;
            if(res.data.gender === 0){
              this.form.gender = '男';
            }else if(res.data.gender === 1){
              this.form.gender = '女';
            }else{
              this.form.gender = '未知';
            }
        });
    }
};
</script>
<style scoped>
.header {
    position: relative;
    box-sizing: border-box;
    width: 100%;
    height: 70px;
    font-size: 22px;
    color: #fff;
}
.collapse-btn {
    float: left;
    padding: 0 21px;
    cursor: pointer;
    line-height: 70px;
}
.header .logo {
    float: left;
    width: 400px;
    line-height: 70px;
}
.header-right {
    width: 854px!important;
    float: right;
    padding-right: 50px;
}
.header-user-con {
    display: flex;
    height: 70px;
    align-items: center;
}
.btn-fullscreen {
    transform: rotate(45deg);
    margin-right: 5px;
    font-size: 24px;
}
.btn-bell,
.btn-fullscreen {
    line-height: 50px!important;
    position: relative;
    width: 30px;
    height: 30px;
    text-align: center;
    border-radius: 15px;
    cursor: pointer;
}
.btn-bell-badge {
    position: absolute;
    right: 0;
    top: -2px;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background: #f56c6c;
    color: #fff;
}
.btn-bell .el-icon-bell {
    color: #fff;
}
.user-name {
    margin-left: 10px;
}
.user-avator {
    margin-left: 20px;
}
.user-avator img {
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
}
.el-dropdown-link {
    color: #fff;
    cursor: pointer;
}
.el-dropdown-menu__item {
    text-align: center;
}
</style>
